package gl.android.biyiqing.fragment;

import gl.android.biyiqing.R;
import gl.android.biyiqing.api.API;
import gl.android.biyiqing.bean.BYQUserInfo;
import gl.android.biyiqing.competent.OnToUserInfoItemClickListener;
import gl.android.biyiqing.event.EventGreet;
import gl.android.biyiqing.event.EventSendGift;
import gl.common.AjaxCallBackProxy;
import gl.common.BaseControl;
import gl.common.UserManger;
import gl.uitls.Tip;

import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import de.greenrobot.event.EventBus;

/**
 * 消息盒子
 * 
 * @author geliang
 * 
 */
public class WangLaiListFragment extends BaseListFragmet implements
		OnClickListener {

	protected final String ERR = "发送消息失败";
	protected final String OK = "发送成功";
	private TextView empty;
	private AbsListView list;
	protected String NOTFOUND = "没有消息";
	private Drawable drawable;
	private EditText et_msg;
	private View btn_send;
	private RadioGroup mRadioGroup;
	private ViewPager pager;

	// private PullToRefreshView pullToRefreshView1;

	private void bindViews() {
		empty = (TextView) findViewById(R.id.empty);
		et_msg = (EditText) findViewById(R.id.et_msg);

		list = (AbsListView) findViewById(android.R.id.list);

		list.setEmptyView(empty);
		empty.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				initdata();
			}
		});
		empty.setText(NOTFOUND);
		empty.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
				null);
	}

	public WangLaiListFragment() {
	}

	String urls[] = { "GetUserGreetList?userid=%s&type=2",
			"GetUserGreetList?userid=%s&type=1",
			"GetGiftDealings?userid=%s&type=1",
			"GetGiftDealings?userid=%s&type=0" };
	private int messageType;
	public static final String key = "messageType";
	
	public void onEvent(EventSendGift mEventSendGift){
		initdata();
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		messageType = Integer.parseInt(getArguments().getString(KEY_USER_ID));
		EventBus.getDefault().register(this);
		switch (messageType) {
		case 0:
			rootView = inflater.inflate(R.layout.fragment_messagelist,
					container, false);
			break;
		case 1:
			rootView = inflater.inflate(R.layout.fragment_messagelist,
					container, false);
			break;
		case 2:
			rootView = inflater.inflate(R.layout.gridview_empty, container,
					false);
			break;
		case 3:
			rootView = inflater.inflate(R.layout.gridview_empty, container,
					false);
			break;

		default:
			break;
		}
		drawable = getActivity().getResources().getDrawable(
				R.drawable.ic_talking_btn_mail_p);
		bindViews();

		initdata();
		return rootView;
	}

	private void initdata() {
		String[] from = new String[] { "username", BYQUserInfo.KEY_SHENFEN,
				"msgcount", "stime", "avatar" };
		int[] to = new int[] { R.id.tv_nick, R.id.age, R.id.tv_jianjie,
				R.id.time, R.id.iv_avator };
		int res = R.layout.item_list_mygreet;
		switch (messageType) {
		case 0:
			from = new String[] { "username", BYQUserInfo.KEY_SHENFEN,
					"greeting", "sendtime", "avatar" };
			to = new int[] { R.id.tv_nick, R.id.age, R.id.tv_jianjie,
					R.id.time, R.id.iv_avator };
			res = R.layout.item_list_mygreet;
			break;

		case 1:
			from = new String[] { "username", BYQUserInfo.KEY_SHENFEN,
					"greeting", "sendtime", "avatar" };
			to = new int[] { R.id.tv_nick, R.id.age, R.id.tv_jianjie,
					R.id.time, R.id.iv_avator };
			res = R.layout.item_list_mygreet;
			break;
		case 2:
			// giftname
			from = new String[] { "giftname", "username", "imgurl", "sendtime",
					"avatar" };
			to = new int[] { R.id.tv_nick, R.id.age, R.id.iv_avator, R.id.time,
					R.id.gift };
			res = R.layout.item_list_mygift;
			break;
		case 3:
			// giftname
			from = new String[] { "giftname", "username", "imgurl", "sendtime",
					"avatar" };
			to = new int[] { R.id.tv_nick, R.id.age, R.id.iv_avator, R.id.time,
					R.id.gift };
			res = R.layout.item_list_mygift;
			break;

		default:
			break;
		}

		initAbsListView(getListView(), API.URL + urls[messageType],
				new Object[] { UserManger.getInstance().userid }, res, from, to);

	}
	public void onEvent(EventGreet event){
		if (messageType == 1) {
			initdata();
		}
	}
	@Override
	public void onDestroyView() {
		super.onDestroyView();
		EventBus.getDefault().unregister(this);
	}
	@Override
	protected String preFormatJsonData(String t) {
		return BYQUserInfo.preFormatShenFen(t);
	}
	@Override
	public Object getOnItemListener(String[] from, String t)
			throws JSONException {
		switch (messageType) {
		case 0:
//			return new OnMsgItemClickListener(t, messageType);
//			return null;
			return new OnToUserInfoItemClickListener(getActivity(),
					new JSONObject(t).getJSONArray("data").toString(), null);
		case 1:
			return new OnToUserInfoItemClickListener(getActivity(),
					new JSONObject(t).getJSONArray("data").toString(), null);
		case 2:
			return new OnToUserInfoItemClickListener(getActivity(),
					new JSONObject(t).getJSONArray("data").toString(), null);
		case 3:
			return new OnToUserInfoItemClickListener(getActivity(),
					new JSONObject(t).getJSONArray("data").toString(), null);

		}
		return super.getOnItemListener(from, t);
	}

	@Override
	public void onClick(View v) {
		Editable input = et_msg.getText();
		if (TextUtils.isEmpty(input)) {
			return;
		}
		Object[] args = new Object[] { UserManger.getInstance().userid,
				UserManger.getInstance().userid, input.toString() };
		BaseControl.get(API.URL
				+ "SendMessage?fuserid=%s&tuserid=%s&content=%s",
				new AjaxCallBackProxy() {
					@Override
					public void onSuccess(String t) {
						super.onSuccess(t);
						try {
							if ("0".equals(new JSONObject(t).getString("msg"))) {
								Tip.show(ERR);
								return;
							}
							if ("1".equals(new JSONObject(t).getString("msg"))) {
								Tip.show(OK);
								return;
							}
						} catch (Exception e) {
							e.printStackTrace();
							onFailure(e, 0, e.getMessage());
						}
					}

					@Override
					public void onFailure(Throwable t, int errorNo,
							String strMsg) {
						super.onFailure(t, errorNo, strMsg);
						Tip.show(ERR);
					}
				}, args);
	}
}